package com.nubia.service;


import com.nubia.dao.UserDao;
import com.nubia.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public void register(User user) {
        User result_user = userDao.findUserByName(user.getUser_name());
        if(result_user == null)
        {
            userDao.insertUser(user);
        }else {
          throw new RuntimeException("非程序错误，抛至controller");
        }
    }
//   登录
    @Override
    public User login(User user) {
        User userLG = userDao.findUserByName(user.getUser_name());
        //根据用户进行查询
        if (!ObjectUtils.isEmpty(userLG)){
            if (userLG.getPassword().equals(user.getPassword())){
                return userLG;
            }else {
                throw new RuntimeException("密码不正确");
            }
        }else {
            throw new RuntimeException("用户名输入错误");
        }


    }
}
